﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


namespace WebDesign
{

    public partial class Ordering : System.Web.UI.Page
    {
        static int Step;
        static int CustomerID;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnWelcomeNext_Click(object sender, EventArgs e)
        {
            if (PersonTypeDropDown.SelectedIndex == 0)
            {
                Step = 1;
                multiviewOrdering.ActiveViewIndex = 1;
            }
            else
            {
                Step = 2;
                multiviewOrdering.ActiveViewIndex = 2;
            }
        }

        protected void btnRealPersonNext_Click(object sender, EventArgs e)
        {


            multiviewOrdering.ActiveViewIndex = 3;
        }

        protected void btnRealPersonPrevious_Click(object sender, EventArgs e)
        {
            multiviewOrdering.ActiveViewIndex = 0;
        }

        protected void txtOrderExp_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btnLegPropertiseNext_Click(object sender, EventArgs e)
        {

            multiviewOrdering.ActiveViewIndex = 3;
        }

        protected void btnLegPropertisePrevious_Click(object sender, EventArgs e)
        {
            multiviewOrdering.ActiveViewIndex = 0;
        }

        protected void btnOrderPrevious_Click(object sender, EventArgs e)
        {
            if (Step == 1)
                multiviewOrdering.ActiveViewIndex = 1;
            else
                multiviewOrdering.ActiveViewIndex = 2;
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["KaveConnString"].ToString());
            //new SqlConnection("Data Source=.;Initial Catalog=KaveComp;Integrated Security=True");
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandType = CommandType.Text;
            SqlDataReader dr;
            con.Open();


            




            if (Step == 1)
            {
                com.CommandText = "insert into Customertbl(CustomerName,CustomerFamily,CustomerPhone,CustomerEmail,CustomerAddress,Reagent)values(@CustomerName,@CustomerFamily,@CustomerPhone,@CustomerEmail,@CustomerAddress,@Reagent);select top 1 CustomerId from Customertbl order by CustomerId desc";
                com.Parameters.AddWithValue("@CustomerName", txtFirstName.Text);
                com.Parameters.AddWithValue("@CustomerFamily", txtLastName.Text);
                com.Parameters.AddWithValue("@CustomerPhone", txtPhone.Text);
                com.Parameters.AddWithValue("@CustomerEmail", txtEmail.Text);
                com.Parameters.AddWithValue("@CustomerAddress", txtaddress.Text);
                com.Parameters.AddWithValue("@Reagent", txtReagent.Text);

                dr = com.ExecuteReader();
                dr.Read();
                CustomerID = int.Parse(dr["CustomerId"].ToString());
                dr.Close();

                com.Parameters.Clear();
                com.CommandText = "insert into RealCustomertbl(CustomerId,CustomerIdentityNum,CustomerFatherName,CustomerNumNationalId,CustomerMobile)values(@CustomerId,@CustomerIdentityNum,@CustomerFatherName,@CustomerNumNationalId,@CustomerMobile)";//,CustomerCity

                com.Parameters.AddWithValue("@CustomerId", CustomerID);
                com.Parameters.AddWithValue("@CustomerIdentityNum", txtIdentityNum.Text);
                com.Parameters.AddWithValue("@CustomerFatherName", txtFatherName.Text);
                com.Parameters.AddWithValue("@CustomerNumNationalId", txtNumNationalId.Text);
                com.Parameters.AddWithValue("@CustomerMobile", txtMobile.Text);
              //  com.Parameters.AddWithValue("@CustomerCity", txtCustomerCity.Text);

                com.ExecuteNonQuery();
            }

            else
            {
                //اپلود فایل

               /* HttpPostedFile officfile = ((FileUpload)viewOrder.FindControl("FileUpload1")).PostedFile;
                officfile.SaveAs(Server.MapPath("~/officefile/file"));
                string url = "~/officefile/file";*/

                com.CommandText = "insert into Customertbl(CustomerName,CustomerFamily,CustomerPhone,CustomerEmail,CustomerAddress,Reagent)values(@CustomerName,@CustomerFamily,@CustomerPhone,@CustomerEmail,@CustomerAddress,@Reagent);select top 1 CustomerId from Customertbl order by CustomerId desc";
                com.Parameters.AddWithValue("@CustomerName", txtFirstLegalName.Text);
                com.Parameters.AddWithValue("@CustomerFamily", txtLastLegalName.Text);
                com.Parameters.AddWithValue("@CustomerPhone", txtCompanyPhone.Text);
                com.Parameters.AddWithValue("@CustomerEmail", txtCompanyEmail.Text);
                com.Parameters.AddWithValue("@CustomerAddress", txtCompanyAddress.Text);
                com.Parameters.AddWithValue("@Reagent", txtCompanyReagen.Text);
                //com.Parameters.AddWithValue("@FileUpload",url);

               

                dr = com.ExecuteReader();
                dr.Read();
                CustomerID = int.Parse(dr["CustomerId"].ToString());
                dr.Close();

                com.Parameters.Clear();

                com.CommandText = "insert into LegalCustomertbl(CustomerId,CompanyName,CompanyActivity)values(@CustomerId,@CompanyName,@CompanyActivity)";
               
                com.Parameters.AddWithValue("@CustomerId", CustomerID);
                com.Parameters.AddWithValue("@CompanyName", txtCompanyName.Text);
                com.Parameters.AddWithValue("@CompanyActivity",txtCompanyActivity.Text );

                com.ExecuteNonQuery();
            }

            com.Parameters.Clear();

            com.CommandText = "insert into Ordertbl(TypeId,OrderExp,CustomerId,OrderDatefinal,OrderingDate)values(@TypeId,@OrderExp,@CustomerId,@OrderDatefinal,@OrderingDate)";//,
            com.Parameters.AddWithValue("@TypeId", int.Parse(DropDownOrderType.SelectedValue));
            com.Parameters.AddWithValue("@OrderExp", txtOrderExp.Text);
            com.Parameters.AddWithValue("@CustomerId", CustomerID);
            com.Parameters.AddWithValue("@OrderDatefinal", txtOrderDataFinal.Text);
            com.Parameters.AddWithValue("@OrderingDate", System.DateTime.Now.Date.ToString());

            com.ExecuteNonQuery();
            con.Close();
        }


    }
}